package cn.ac.iscas.action.patent;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;


import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.patent.Patent;
import cn.ac.iscas.service.patent.IPatentServive;

@Controller @Scope("prototype")
public class PatentAction {
	PatentQuery patentQuery;
	
	public PatentQuery getPatentQuery() {
		return patentQuery;
	}

	public String pictureDetail()
	{
		
		return "pictureDetail";
	}
	public void setPatentQuery(PatentQuery patentQuery) {
		this.patentQuery = patentQuery;
	}
	@Resource IPatentServive patentService;
	public IPatentServive getPatentService() {
		return patentService;
	}

	public void setPatentService(IPatentServive patentService) {
		this.patentService = patentService;
	}
	private int page;    
	

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	public String typeIn()
	{
		return "add";
	}
	public String list()
	{
		page = (0==page)?1:page;
		PageView<Patent> pageView = new PageView<Patent>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(patentService.getScrollData(Patent.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list";
	}
	public String query(){
		if(page<=0)page=1;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		
		if(patentQuery.getPatentName()!=null && !"".equals(patentQuery.getPatentName())){
			if(params.size()>0) jpql.append(" and ");
			System.out.println(patentQuery.getPatentName()+"name");
			jpql.append(" o.patentName like ?");
			params.add("%"+ patentQuery.getPatentName()+ "%");
		}
		if(patentQuery.getAcceptNo()!=null && !"".equals(patentQuery.getAcceptNo())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.acceptNo like ?");
			params.add("%"+patentQuery.getAcceptNo()+"%");
		}
		if(patentQuery.getPatentNo()!=null && !"".equals(patentQuery.getPatentNo())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.patentNo like ?");
			params.add("%"+patentQuery.getPatentNo()+"%");
		}
		System.out.println(patentQuery.getPatentee()+"ddd");
		if(patentQuery.getPatentee()!=null && !"".equals(patentQuery.getPatentee())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.patentee like ?");
			params.add("%"+patentQuery.getPatentee()+"%");
		}
		if(patentQuery.getState()!=null &&!"".equals(patentQuery.getState()))
		{
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.state = ?");
			params.add(patentQuery.getState());
		}
		PageView<Patent> pageView=new PageView<Patent>(7,page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(patentService.getScrollData(Patent.class,pageView.getFirstResult(), 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list" ;
		}
	    public String test() throws IOException
	    {
	    	ActionContext ctx = ActionContext.getContext();     
		    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		    response.setContentType("text/html;charset=utf-8");
		    PrintWriter out=response.getWriter();
		    response.setContentType("text/html;charset=utf-8");
		    HttpServletRequest request = ServletActionContext.getRequest();
		    System.out.println(request.getParameter("name"));
		    out.println(request.getParameter("name") + ",服务器时间：" + new Date()
		    		+ ",请校对!");
		   out.flush();
		   out.close();
		   return null;
	    }
}
